import user from '../../../utils/user'
import {
    getPlanInfo,
    getPlanRecord,
    renewalMonthNotify,
    getPeopleList,
    checkperiodareaApi
} from '../../api/long-insure'
import {
    dateFormat,
    toast,
    shake
} from '../../../utils/util'
Page({
    data: {
        insurCompany: '',
        showerror1msg: '非常抱歉，当前方案不支持该（天津）地区投保，请选择"其他方案"继续投保',
        showerror1: false,
        id: null,
        detail: {},
        planPeoples: [],
        record: [],
        canInsureRenewal: false,
        dateMonth: '',
        isGoFluctuate: false,
        planStatus: '1',
        preciseSearchUserInfo: ''
    },
    static: {
        pageNum: 1,
        hasNext: true
    },
    async onLoad(options) {

        this.setData({
            id: options.id
        })
    },
    async onShow() {
        await user.waitLogin()
        this.setData({
            planPeoples: [],
            dateMonth: dateFormat('YYYY-mm', new Date())
        })
        this.static.pageNum = 1
        this.static.hasNext = true
        this.getPlanInfo()
        this.getPlanPeople()
        this.getPlanRecord()
        this.getRenewalMonthNotify()
    },
    onReachBottom() {
        this.static.pageNum += 1
        this.getPlanPeople()
    },
    // 获取最近5条加减保记录
    async getPlanRecord() {
        const result = await getPlanRecord({
            companyPlanId: this.data.id,
            pageNum: 1,
            pageSize: 5,
        })
        // 先清在加因为使用的组件 所以 需要组件进行刷新
        this.setData({
            record: []
        }, () => {
            this.setData({
                record: result.list
            })
        })
    },
    // 获取保单员工列表（分页）
    async getPlanPeople(clear) {
        if (clear) {
            this.static.pageNum = 1
            this.static.hasNext = true
            this.data.planPeoples = []
        }
        if (!this.static.hasNext) {
            return
        }
        const result = await getPeopleList({
            companyPlanId: this.data.id,
            pageNum: this.static.pageNum,
            pageSize: 20,
            searchText: '',
            searchUserInfo: this.data.preciseSearchUserInfo,
            policyStatus: this.data.planStatus == 'null' ? '' : this.data.planStatus
        })
        this.static.hasNext = result.hasNextPage
        this.data.planPeoples.push(...result.list)
        this.setData({
            planPeoples: this.data.planPeoples
        })
    },
    // 获取保单详情
    async getPlanInfo() {
        const result = await getPlanInfo({
            planId: this.data.id,
            month: this.data.dateMonth
        })
        result.beginTime = result.beginTime.split(' ')[0]
        result.endTime = result.endTime ? result.endTime.split(' ')[0] : ''
        // result.isEnd = result.endTime ? new Date(result.endTime).getTime() < new Date().getTime() : false
        console.log(result.insurCompany, 'result.insurCompany')
        this.data.insurCompany = result.insurCompany
        if (result.insurCompany === 'period_002_xdc') {
            const startTime = new Date('2022-09-30 18:00').getTime()
            const endTime = new Date('2022-09-30 24:00').getTime()
            const currentTime = new Date().getTime()
            if ((currentTime > startTime) && (currentTime < endTime)) {
                this.setData({
                    isGoFluctuate: true
                })
            }
            console.log(startTime, 'starTime')
            console.log(endTime, 'endTime')
            console.log(currentTime, 'currentTime')
        }
        this.setData({
            detail: result
        })
    },
    // 跳转到加保
    onInsureAdd() {
        checkperiodareaApi({
            planId: this.data.id
        }).then((res) => {
            console.log(res, 'fasdguyfghuys')
            if (res.isAreaError && !res.isWhitelist) {
                this.setData({
                    showerror1: true,
                })
                return
            }
            if (this.data.isGoFluctuate) {
                toast('当前时间段不可操作')
                return
            }
            wx.navigateTo({
                url: '/packageLongInsure/pages/add-subtract/add-subtract?type=add&id=' + this.data.id + '&code=' + this.data.insurCompany,
            })
        })
    },
    popcancelorder1() {
        this.setData({
            showerror1: false,
        })
    },
    popsureorder1() {
        this.setData({
            showerror1: false,
        })
        wx.navigateBack()
    },
    onInsureRenewAdd() {
        if (this.data.isGoFluctuate) {
            toast('当前时间段不可操作')
            return
        }
        wx.navigateTo({
            url: '/packageLongInsure/pages/add-subtract/add-subtract?type=add&id=' + this.data.id + '&renew=true',
        })
    },
    // 跳转到减保
    onInsureSubtract() {
        if (this.data.isGoFluctuate) {
            toast('当前时间段不可操作')
            return
        }
        wx.navigateTo({
            url: '/packageLongInsure/pages/reduce-insure/reduce-insure?type=checkbox&id=' + this.data.id + '&code=' + this.data.insurCompany,
        })
    },
    // 更多加减保记录
    onRecordMore() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/add-subtract-record/add-subtract-record?id=' + this.data.id,
        })
    },
    // 跳转到统计界面
    onStatistics() {
        wx.reportEvent('statistics', {})
        wx.navigateTo({
            url: '/packageLongInsure/pages/statistics/statistics?id=' + this.data.id,
        })
    },
    async getRenewalMonthNotify() {
        const result = await renewalMonthNotify({
            planId: this.data.id
        })
        console.log(result, 'result月份检测')
        this.setData({
            canInsureRenewal: result.canInsureRenewal
        })
    },
    // 选择月的回调
    async onSelectMonth(event) {
        this.setData({
            dateMonth: event.detail.value
        })
        await this.getPlanInfo()
    },
    async onRevoke() {
        console.log('父回调')
        await this.getPlanRecord()
        this.getPlanInfo()
    },
    onPlanStatus(event) {
        const {
            value
        } = event.currentTarget.dataset
        this.setData({
            planStatus: value
        })
        this.getPlanPeople(true)
    },
    onSearch(event) {
        const {
            value
        } = event.detail
        shake(() => {
            this.setData({
                preciseSearchUserInfo: value
            })
            this.getPlanPeople(true)
        })
    },
    // 去账单页面 packageLongInsure/pages/bill/bill
    onGoBill() {
        wx.navigateTo({
            url: '/packageLongInsure/pages/bill/bill?id=' + this.data.id,
        })
    }
})